﻿using System;
using System.IO;
using System.Data;
using System.Windows;
using System.Collections;
using System.Windows.Input;
using System.ComponentModel;
using System.Windows.Controls;
using Sinacor.Infra.UI.Common;
using Sinacor.Infra.UI.Controls;
using Sinacor.Infra.UI.Controls.SmartClient;
using System.Collections.Generic;
using Sinacor.Infra.UI.Controls.SmartClient.Validation;
using Sinacor.UI.Bovespa.Ordens.View.SmartClient;
using PSE.Framework.UI.Resources;

namespace Sinacor.UI.Bovespa.Parametro.View.SmartClient
{
    /// <summary>
    /// Classe CadastroCustodianteADR
    /// </summary>
    public partial class CadastroCustodianteADR : SinacorWindow, Presenter.IADRCustodiante
    {
        #region Atributos

        private Presenter.ADRCustodiante presenter;
        ModelCollection<Model.ADR> _listaADR = new ModelCollection<Sinacor.UI.Bovespa.Parametro.Model.ADR>();
        int selected;


        #region Construtor

        /// <summary>
        /// CadastroCustodianteADR
        /// </summary>
        public CadastroCustodianteADR()
        {
            InitializeComponent();
            this.DtgCustodiante.NewItemType = typeof(Model.Custodiante);
            this.DtgADR.NewItemType = typeof(Model.ADR);
            this.presenter = new Presenter.ADRCustodiante(this);
            this.DataContext = this.presenter;
            this.AfterInitialize += new RoutedEventHandler(CadastroCustodianteADR_AfterInitialize);
            this.Closing += new System.ComponentModel.CancelEventHandler(CadastroCustodianteADR_Closing);
        }
        #endregion Atributos
        /// <summary>
        /// CadastroCustodianteADR_Closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        void CadastroCustodianteADR_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.DtgADR.EndUpdate();
            this.DtgCustodiante.EndInsert();
        }

        /// <summary>
        /// CadastroCustodianteADR_AfterInitialize
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CadastroCustodianteADR_AfterInitialize(object sender, RoutedEventArgs e)
        {
            this.presenter.Inicializar();
        }
        #endregion Construtor

        #region IADRCustodiante Members

        /// <summary>
        /// AtualizarGridADR
        /// </summary>
        public void AtualizarGridADR()
        {
            this.DtgADR.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
        }
        /// <summary>
        /// AtualizarGridCustodiantes
        /// </summary>
        public void AtualizarGridCustodiantes()
        {
            this.DtgCustodiante.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
        }

        public bool ConfirmarExclusaoADR()
        {
            return (SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("FaturamentoBovespa.Cadastros.Message.SolConfirmacaoExclusao"), SinacorMessageBoxButton.YesNo, SinacorMessageBoxImage.Question) == SinacorMessageBoxResult.Yes);
        }

        public bool ConfirmarExclusaoCustodiante()
        {
            return (SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("FaturamentoBovespa.Cadastros.Message.ConfirmarExclCustodiante"), SinacorMessageBoxButton.YesNo, SinacorMessageBoxImage.Question) == SinacorMessageBoxResult.Yes);
        }

        public void InformarExclusaoCustodianteSucesso()
        {
            SinacorMessageBox.Show(FindResource("FaturamentoBovespa.Cadastros.Message.ExclusaoRealizada").ToString(), SinacorMessageBoxImage.Information);
        }

        public void InformarInclusaoCustodianteSucesso()
        {
            SinacorMessageBox.Show(FindResource("FaturamentoBovespa.Cadastros.Message.CustodianteIncluido").ToString(), SinacorMessageBoxImage.Information);

        }

        public void InformarNenhumRegistroEncontrado()
        {
            SinacorMessageBox.Show(FindResource("FaturamentoBovespa.Cadastros.Message.DadosNaoEncontrados").ToString(), SinacorMessageBoxImage.Information);

        }

        /// <summary>
        /// InformarOperacaoSucesso
        /// </summary>
        public void InformarOperacaoSucesso()
        {
            SinacorMessageBox.Show(FindResource("FaturamentoBovespa.Cadastros.Message.ADRIncluido").ToString(), SinacorMessageBoxImage.Information);
        }

        #endregion IADRCustodiante Members

        #region Eventos

        private void BtnFiltrar_Click(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is SinacorButton)
            {
                this.ShowUserWaitingProcess();
                this.ClearServiceErrors();
                this.presenter.ConsultarCustodiantes();
                this.AtualizarGridCustodiantes();

                if (this.presenter.ListaCustodiantes.Count > 0)
                {
                    this.DtgCustodiante.SelectFirstItem();
                    this.presenter.FiltroADR.CodigoCustodiante = this.presenter.ListaCustodiantes[0].Codigo;
                    this.presenter.ConsultarADR();
                }
                else
                {
                    this.presenter.ListaADR.Clear();
                    this.InformarNenhumRegistroEncontrado();
                }

                this.AtualizarGridADR();
                this.HideUserWaitingProcess();
            }
        }

        private void BtnDesfazerFiltro_Click(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is SinacorButton)
            {
                this.TxtAtivo.Text = string.Empty;
                this.presenter.FiltroCustodiante.Ativo = string.Empty;
                this.ShowUserWaitingProcess();
                this.ClearServiceErrors();
                this.presenter.ConsultarCustodiantes();
                this.AtualizarGridCustodiantes();

                if (this.presenter.ListaCustodiantes != null && this.presenter.ListaCustodiantes.Count > 0)
                {                    
                    this.presenter.FiltroADR.CodigoCustodiante = null;
                    this.presenter.ConsultarADR();
                }
                else
                {
                    this.presenter.ListaADR.Clear();
                }

                this.AtualizarGridADR();
                this.HideUserWaitingProcess();
            }
        }

        private void DtgCustodiante_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.DtgCustodiante.SelectedIndex != -1)
            {
                ShowUserWaitingProcess();
                this.presenter.FiltroADR.CodigoCustodiante = ((Model.Custodiante)this.DtgCustodiante.SelectedItem).Codigo;
                this.presenter.ConsultarADR();                
                this.AtualizarGridADR();
            }
            else
            {                
                this.AtualizarGridADR();                
            }
            HideUserWaitingProcess();
        }

        private void DtgCustodiante_EndingInsert(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                if (DtgCustodiante.GetInsertionRow() != null)
                {
                    this.ClearServiceErrors();
                    ShowUserWaitingProcess();
                    this.presenter.Custodiante = DtgCustodiante.EditItem as Model.Custodiante;
                    if (this.presenter.IncluirCustodiante())
                    {
                        this.InformarInclusaoCustodianteSucesso();
                        this.ClearChanges();
                        this.presenter.ConsultarADR();

                        if (this.ErrorMessages.Count > 0)
                        {
                            e.Handled = true;
                            e.Cancel = true;
                        }
                    }
                    else
                    {
                        e.Handled = true;
                        e.Cancel = true;
                    }
                    this.HideUserWaitingProcess();
                }
            }
        }


        private void DtgCustodiante_Deleting(object sender, CancelItemsEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                this.ClearServiceErrors();

                if (DtgCustodiante.SelectedItem != null)
                {
                    this.ClearChanges();
                    ShowUserWaitingProcess();

                    this.presenter.Custodiante = this.DtgCustodiante.SelectedItem as Model.Custodiante;

                    if (this.presenter.ExcluirCustodiante())
                    {
                        this.AtualizarGridCustodiantes();
                        this.InformarExclusaoCustodianteSucesso();
                        this.HideUserWaitingProcess();
                    }
                    else
                    {
                        this.HideUserWaitingProcess();
                        e.Handled = true;
                        e.Cancel = true;
                    }
                }
            }
        }
        private void BtnAtivo_Click(object sender, RoutedEventArgs e)
        {

            Ordens.Model.Titulo modelTitulo = new Ordens.Model.Titulo();
            modelTitulo.Codigo = TxtAtivo.Text;
            modelTitulo.Mercado = null;
            modelTitulo.DataPregao = null;

            PesquisaTitulo pesquisaTitulo = new PesquisaTitulo();
            pesquisaTitulo.CarregarParametros(modelTitulo, null);
            pesquisaTitulo.Owner = this;
            pesquisaTitulo.SetMenuHeader("Bovespa.Ordens.Label.ManutencaoOrdens");
            pesquisaTitulo.ShowDialog();

            if (pesquisaTitulo.TituloEscolhido != null)
            {
                presenter.FiltroCustodiante.Ativo = pesquisaTitulo.TituloEscolhido.Codigo;
                TxtAtivo.UpdateTarget(SinacorTextBox.TextProperty);
            }
        }
        

        private void DtgCustodiante_Canceling(object sender, CancelItemEventArgs e)
        {
            this.ClearServiceErrors();
            this.ClearChanges();
        }

        #endregion Eventos

        private void DtgADR_SavingAll(object sender, CancelItemsEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                this.ClearServiceErrors();

                this.ClearChanges();
                ShowUserWaitingProcess();
                if (this.DtgCustodiante.SelectedItem != null)
                {
                    this.presenter.Custodiante = this.DtgCustodiante.SelectedItem as Model.Custodiante;
                }
                else
                {
                    this.presenter.Custodiante = this.presenter.ListaCustodiantes[0];
                }
                this.presenter.SalvarADR();
                this.presenter.ConsultarADR();
                if (this.HasErrors)
                {
                    e.Handled = true;
                    e.Cancel = true;
                }
                this.HideUserWaitingProcess();
            }                                       
        }

        private void DtgADR_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.DtgADR.SelectedIndex != -1)
            {
                selected = this.DtgADR.SelectedIndex;
                ShowUserWaitingProcess();
                this.presenter.ADR = DtgADR.SelectedItem as Model.ADR;
                this.presenter.ConsultarCustodianteADR();                
                this.DtgADR.SelectedIndex = selected;
            }
            HideUserWaitingProcess();
        }

    }
}